package com.valley.boot;



public class ClassLoaderUtil {

    static final String CLASSLOADER_NAME = "org.springframework.boot.loader.LaunchedURLClassLoader";

    public static ClassLoader getClassLoader(ClassLoader loader){
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        if (!CLASSLOADER_NAME.equals(classLoader.getClass().getName())){
            if (CLASSLOADER_NAME.equals(classLoader.getParent().getClass().getName())){
                classLoader = classLoader.getParent();
            }else {
                if (loader != null){
                    if (CLASSLOADER_NAME.equals(loader.getClass().getName())){
                        classLoader = loader;
                    }else if (CLASSLOADER_NAME.equals(loader.getParent().getClass().getName())){
                        classLoader = loader.getParent();
                    }else {
                        classLoader = ClassLoader.getSystemClassLoader();
                    }
                }
            }
        }
        return classLoader;
    }
}
